package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.doamin.cargo.*;
import com.itheima.service.cargo.ExportProductService;
import com.itheima.service.cargo.ExportService;
import com.itheima.vo.ExportProductVo;
import com.itheima.vo.ExportResult;
import com.itheima.vo.ExportVo;
import com.itheima.web.controller.BaseController;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("cargo/export")
public class ExportController extends BaseController{
    @Reference
    ExportService exportService;
    @Reference
    ExportProductService exportProductService;

    /**
     * 进行电子保运
     * @return
     */
    @RequestMapping("exportE")
    public String toExportE(String id) throws Exception {
        //获得所有保运单
        Export export = exportService.findById(id);
        //将报运单中的数据封装到电子报运中
        ExportVo exportVo=new ExportVo();
        BeanUtils.copyProperties(export,exportVo);
        exportVo.setExportId(export.getId());
        //通过报单id获得报单货物
        //条件对象
        ExportProductExample exportProductExample=new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProducts = exportProductService.findAll(exportProductExample);
        //对电子货物报单进行封装
        List<ExportProductVo> exportProductVos=new ArrayList<>();
        for (int i=0;i<exportProducts.size();i++){
            ExportProductVo exportProductVo=new ExportProductVo();
            //进行数据封装
            BeanUtils.copyProperties(exportProducts.get(i),exportProductVo);
            exportProductVo.setExportProductId(exportProducts.get(i).getId());
            exportProductVos.add(exportProductVo);
        }
        //报运单与报运单货物关联
        exportVo.setProducts(exportProductVos);
        String url="http://localhost:8081/ws/export";
        //到海关进行保存
        WebClient.create(url).type(MediaType.APPLICATION_JSON_TYPE).post(exportVo);
        ExportResult exportResult = WebClient.create(url + "/" + id).get(ExportResult.class);
        exportService.updateE(exportResult);
        return "redirect:/cargo/export/list";
    }

    //查询出口报运表的数据
    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "4") int size){
        //创建查询条件
        ExportExample exportExample=new ExportExample();
        exportExample.createCriteria().andCompanyIdEqualTo(getCurrentUser().getCompanyId());
        exportExample.setOrderByClause("create_time desc");
        //调用service进行查询
        request.setAttribute("page",exportService.findAll(exportExample,page,size));
        return "cargo/export/export-list";
    }

    //查询已报运合同
    @RequestMapping("contractList")
    public String contractList(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "2") int size){
        //创建条件
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andStateEqualTo(1).andCompanyIdEqualTo(getCurrentUser().getCompanyId());
        //调用service层方法
        PageInfo pageInfo=exportService.findAll(contractExample,page,size);
        request.setAttribute("page",pageInfo);
        return "/cargo/export/export-contractList";
    }

    //多个带报运合同进行数据封装，变成已报运状态,此控制器方法进行页面跳转
    @RequestMapping("toExport")
    public String toExport(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }

    @RequestMapping("edit")
    public String toEdit(Export export){
        if (UtilFuns.isEmpty(export.getId())){
            export.setCompanyId(getCurrentUser().getCompanyId());
            export.setCompanyName(getCurrentUser().getCompanyName());
            exportService.save(export);
        }else {
            exportService.update(export);
        }
        //调用service方法进行处理

        return "redirect:/cargo/export/list";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(String id){
        //数据回显
        //查询报单数据
        Export export = exportService.findById(id);
        ExportProductExample exportProductExample=new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> eps = exportProductService.findAll(exportProductExample);
        request.setAttribute("export",export);
        request.setAttribute("eps",eps);
        return "cargo/export/export-update";
    }
}
